package com.tanhua.manage.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanhua.dubbo.server.api.UserLikeApi;
import com.tanhua.manage.enums.LogTypeEnum;
import com.tanhua.manage.enums.SexEnum;
import com.tanhua.manage.mapper.LogMapper;
import com.tanhua.manage.mapper.UserInfoMapper;
import com.tanhua.manage.mapper.UserMapper;
import com.tanhua.manage.pojo.Log;
import com.tanhua.manage.pojo.User;
import com.tanhua.manage.pojo.UserInfo;
import com.tanhua.manage.vo.Pager;
import com.tanhua.manage.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserService extends ServiceImpl<UserMapper, User> {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserFreezeService freezeService;

    @Autowired
    private LogMapper logMapper;

    @Reference(version = "1.0.0")
    private UserLikeApi userLikeApi;


    public Pager<UserVo> queryByPage(Integer page, Integer pageSize, Long id, String nickname, String city) {

        IPage<UserVo> iPage = userMapper.queryByPage(new Page<>(page, pageSize), Convert.toStr(id), nickname, city);

        // 对结果的处理
        iPage.getRecords().forEach(userVo -> {
            // 性别的转化
            userVo.setSex(SexEnum.getSexByValue(Integer.parseInt(userVo.getSex())));

            // 用户的状态
            userVo.setUserStatus(freezeService.getFreezeStatusByUserId(userVo.getId()) ? "2" : "1");
        });

        return new Pager<>(iPage);
    }


    public UserVo queryUserInfo(Long userId) {
        UserVo userVo = new UserVo();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        userInfoMapper.selectOne(queryWrapper);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);

        //复制属性值
        BeanUtil.copyProperties(userInfo, userVo);
        BeanUtil.copyProperties(userMapper.selectById(userId), userVo);
        userVo.setNickname(userInfo.getNickName());

        userVo.setUserStatus(freezeService.getFreezeStatusByUserId(userVo.getId()) ? "2" : "1");
        // 喜欢数、粉丝数、配对数
        userVo.setCountBeLiked(userLikeApi.queryFanCount(userId));
        userVo.setCountLiked(userLikeApi.queryLikeCount(userId));
        userVo.setCountMatching(userLikeApi.queryEachLikeCount(userId));

        QueryWrapper query = new QueryWrapper();
        query.eq("user_id", userId);
        query.orderByDesc("created");
        query.last("limit 1");
        Log log = logMapper.selectOne(query);
        if (log != null) {
            userVo.setLastActiveTime(log.getCreated().getTime());
        }

        QueryWrapper query2 = new QueryWrapper();
        query2.eq("user_id", userId);
        query2.eq("type", LogTypeEnum.LOGIN.getValue());
        query2.orderByDesc("created");
        query2.last("limit 1");
        Log log2 = logMapper.selectOne(query2);
        if (log2 != null) {
            userVo.setLastLoginLocation(log2.getPlace());
        }
        return userVo;
    }
}
